<?php
//dragon.hu add @2016.3.2  vipstatistics 

defined('BASEPATH') or die('forbidden');

class Vipstatistics_Controller extends Controller
{
	//private $conn;
	//private $areanum;
	private $areaList;
	private $hostList;
	private $logdb;
	
	function _init()
	{
		check_level(50, segment(0), segment(1));
		set_time_limit(0);
		
		$this->hostList = get_area_list_detal();
		
		$this->areaList = get_area_list_option();

		// log数据库
		$this->logdb = $this->load->database('log_db');
	}
	
	function index()
	{
		if(is_submit())
		{
			$startdate = $_POST['row']['starttime'];
			$enddate = $_POST['row']['endtime'];
			//$serveridtmp = $_POST['row']['area'];
			$serverid = $_POST['areaIds'];
			//$tmp = explode("-", $serveridtmp);
			//$serverid = $tmp[0];
			//var_dump($_POST);
			//print_r($startdate);
			//print_r($enddate);
			//print_r($serverid);
			//exit(0);
			
		}else
		{
			//$startdate = date("Y-m-d");
			//$enddate = date("Y-m-d", strtotime("1 day"));
			$startdate = date("Y-m-d H:i:s", strtotime(date("Y-m-d")));  //默认显示当天
			$enddate = date("Y-m-d H:i:s", time());
			//$serverid = $this->areanum;
			$serverid = array(0);  //默认所有服
			//print_r($startdate);
			//print_r($enddate);
			//exit(0);
		}
		//if (empty($this->areanum)){return;}
		//var_dump($sql); 
		//exit(0);
		
		$datagraph = array();
		$vipgraph = array();  //vip数据
		$rolelogingraph = array();  //活跃vip数据
		
		if (in_array('0', $serverid))
		{
			foreach ($this->hostList as $k => $v) 
			{
				//$sql = "SELECT aft_vip_level, COUNT(aft_vip_level) AS num FROM vip WHERE server_id = $k AND operation_time BETWEEN '$startdate' AND '$enddate' GROUP BY aft_vip_level";
				//$sql = "SELECT a.id,a.role_id,a.aft_vip_level from vip a LEFT JOIN vip b on a.role_id = b.role_id where a.server_id = $k AND a.operation_time BETWEEN '$startdate' AND '$enddate' AND
				//		a.aft_vip_level <= b.aft_vip_level GROUP BY a.id HAVING COUNT(a.id) <= 1 ORDER BY a.role_id,a.aft_vip_level desc";
				$sql = "SELECT DISTINCT(role_id), aft_vip_level FROM vip WHERE server_id = $k AND operation_time BETWEEN '$startdate' AND '$enddate'";
				$sql1 = "SELECT DISTINCT(role_id), viplv FROM rolelogin WHERE server_id = $k AND operation_time BETWEEN '$startdate' AND '$enddate'";
				//var_dump ( $sql );
				/*$tmpconn = getTmpDbConnect ( PLATFORM_ALL, 'all', $k, DBTYPE_LOG );
				if (! $tmpconn) 
				{
					continue;
				}
				$tmpconn->query ($sql);*/
				$this->logdb->query($sql);
				
				while ( $n = /*$tmpconn*/$this->logdb->get_array ( 'default' ) ) 
				{
					if(empty($vipgraph[$k][$n['role_id']]) || $vipgraph[$k][$n['role_id']] < $n['aft_vip_level'])
					{
						$vipgraph[$k][$n['role_id']] = $n['aft_vip_level'];
					}
				}
				//$tmpconn->query($sql1);
				$this->logdb->query($sql1);
				
				while($n = /*$tmpconn*/$this->logdb->get_array('default'))
				{
					if (empty($rolelogingraph[$k]['role_id']) || $rolelogingraph[$k][$n['role_id']] < $n['viplv'])
					{
						$rolelogingraph[$k][$n['role_id']] = $n['viplv'];
					}
				}
			}
		}
		else
		{
			for ($i = 0; $i < count($serverid); $i++)
			{
				$sql = "SELECT DISTINCT(role_id), aft_vip_level FROM vip WHERE server_id = $serverid[$i] AND operation_time BETWEEN '$startdate' AND '$enddate'";
				$sql1 = "SELECT DISTINCT(role_id), viplv FROM rolelogin WHERE server_id = $serverid[$i] AND operation_time BETWEEN '$startdate' AND '$enddate'";
				//var_dump($sql);
				/*$tmpconn = getTmpDbConnect(PLATFORM_ALL, 'all', $serverid[$i], DBTYPE_LOG);
				if (!$tmpconn)
				{
					continue;
				}
				$tmpconn->query($sql);*/
				$this->logdb->query($sql);
				
				while($n = /*$tmpconn*/$this->logdb->get_array('default'))
				{
					if(empty($vipgraph[$serverid[$i]][$n['role_id']]) || $vipgraph[$serverid[$i]][$n['role_id']] < $n['aft_vip_level'])
					{
						$vipgraph[$serverid[$i]][$n['role_id']] = $n['aft_vip_level'];
					}
				}
				//$tmpconn->query($sql1);
				$this->logdb->query($sql1);
				
				$rolelogingraph = array();
				while($n = /*$tmpconn*/$this->logdb->get_array('default'))
				{
					if (empty($rolelogingraph[$serverid[$i]]['role_id']) || $rolelogingraph[$serverid[$i]][$n['role_id']] < $n['viplv'])
					{
						$rolelogingraph[$serverid[$i]][$n['role_id']] = $n['viplv'];
					}
				}
				
			}
		}
		
		$tmpi = 0;  //统计登录的vip总量
		foreach ($rolelogingraph as $key => $value)
		{
			foreach ($value as $k =>$v)
			{
				if ($v == 0) continue;
				$datagraph['avipnum'][$v]['num'] += 1;
				$tmpi++;
			}
		}
		$infograph['allavipnum'] = $tmpi;
		
		foreach ($datagraph['avipnum'] as $k=>$v)
		{
			$datagraph['avipnum'][$k]['rate'] = round($v['num'] / $infograph['allavipnum'], 2);
		}
		
		$infograph['targetdate'] = $startdate.'~'.$enddate;
		
		foreach ($vipgraph as $k=>$v)
		{
			$infograph['allvipnum'] += count($v);
		}
		
		foreach ($vipgraph as $key => $value)
		{
			foreach ($value as $k => $v)
			{
				$datagraph['vipnum'][$v]['num'] += 1;
			}
		}
		
		foreach ($datagraph['vipnum'] as $k=>$v)
		{
			$datagraph['vipnum'][$k]['rate'] = round($v['num'] / $infograph['allvipnum'], 2);
		}
		
		//var_dump($infograph);
		//exit();
		foreach ($datagraph as $k=>$v)
		{
			ksort($v);
			$datagraph[$k] = $v;
		}
		//var_dump($datagraph);
		
		set_area_list_view("arealist");
		$this->view->assign('startdate', $startdate);
		$this->view->assign('enddate', $enddate);
		$this->view->assign('areaList', $this->areaList);
		$this->view->assign('datagraph', $datagraph);
		$this->view->assign('infograph', $infograph);
		
		$this->view->display("gamedata/vipstatistics/index");
	}
	
	
}









?>